package com.yllt4cloud.ddauth.filter;

import com.yllt4cloud.common.ddbase.constant.AuthorityUrlConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Filter;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

public abstract class BaseFilter implements Filter {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseFilter.class);

    private static final String LOGIN_SUFFIX = "login";
    private static final String JS_SUFFIX = ".js";
    private static final String CSS_SUFFIX = ".css";
    private static final String HTML_SUFFIX = ".html";
    private String[] excludedPages = {
            "/printer/",
            "/file/",
            "/clerk/clerk/listClerkZhiWen",
            "/login/dianDanJiShuaKa",
            "/shanmi/huaDan/shuakaShangTai",
            "/system/appAuthorize/appAuthorizeNotice",
            "/ipad/ipadLuoDanJiLu/getVersionMsg                                                                                                                                                                                                                                                                     ersionMsg",
            "/shanmi/shanMi/kaoQin",
            "/ipad/ipadLuoDanJiLu/getZhiFuFangShi",
            "/front",
            "/system/msg/getIpadSkinType",
            "/test",
            "/excel_down_load/",
            "/zike_files/",
            "/services/",
            "/imageStore/"
    };


    public boolean isFilter(ServletRequest request){
      /*  LOGGER.info("请求时间：{}",Calendar.getInstance().getTime() );
        LOGGER.info("请求数据：{}", request.getParameter("data"));
        LOGGER.info("请求uri地址：{}",((HttpServletRequest) request).getRequestURI());
      */
        String url = ((HttpServletRequest) request).getRequestURI().replace(((HttpServletRequest) request).getContextPath(), "").replaceAll("/+","/");
        //过滤掉登陆请求
        if(url.endsWith(LOGIN_SUFFIX)
                || url.endsWith(JS_SUFFIX)
                || url.endsWith(CSS_SUFFIX)
                || url.endsWith(HTML_SUFFIX)
                || url.endsWith(".jpg")
                || url.endsWith(".png")
                || url.endsWith(".xls")
                ){
            return true;
        }

        if(url.contains("endpoint")){
            return true;
        }

        for (String page : excludedPages) {
            if(url.startsWith(page)){
                return true;
            }
        }

        if(!(url.equals(AuthorityUrlConstants.PRINTER_URL_CHUPIN) || url.equals(AuthorityUrlConstants.PRINTER_URL_ZHUANGBINFANG))){
            LOGGER.info("请求IP：{}", request.getRemoteAddr());
            LOGGER.info("请求uri地址：{}", ((HttpServletRequest) request).getRequestURI());
        }
        return false;
    }
}
